<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="客户名称" prop="companyName">
        <el-input
          clearable
          :maxlength="50"
          v-model="queryParams.companyName"
          placeholder="请输入客户名称"
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="contactPhone">
        <el-input
          clearable
          :maxlength="11"
          v-model="queryParams.contactPhone"
          placeholder="请输入手机号"
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select clearable v-model="queryParams.state" placeholder="请选择" class="!w-200px">
          <el-option
            v-for="dict in getIntDictOptionsFn(dict_type.BID_CUSTOMER_STATE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="竞拍权限" prop="biddingState">
        <el-select
          clearable
          v-model="queryParams.biddingState"
          placeholder="请选择"
          class="!w-200px"
        >
          <el-option
            v-for="dict in getIntDictOptionsFn(dict_type.BID_CUSTOMER_BID)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          type="datetimerange"
          class="!w-400px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button type="primary" plain @click="openModel(1)" v-hasPermi="['bid:customer:create']">
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      stripe
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
    >
      <el-table-column label="编号" align="center" width="60" fixed="left">
        <template #default="{ $index }">
          <span>
            {{ $index + 1 }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="客户名称"
        align="center"
        prop="companyName"
        width="200"
        fixed="left"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span class="text-blue-500" @click="openModel(2, scope.row.id)">
            {{ scope.row.companyName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="contactPerson" width="150" />
      <el-table-column label="手机号码" align="center" prop="contactPhone" width="150" />
      <el-table-column label="竞拍权限" align="center" prop="biddingState" width="150">
        <template #default="scope">
          <dict-tag :type="dict_type.BID_CUSTOMER_BID" :value="scope.row.biddingState" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" width="150">
        <template #default="scope">
          <dict-tag :type="dict_type.BID_CUSTOMER_STATE" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column
        label="统一社会信用代码"
        align="center"
        prop="socialCreditCode"
        width="150"
      />
      <el-table-column label="法人姓名" align="center" prop="legalPerson" width="150" />
      <el-table-column
        label="身份证号"
        align="center"
        prop="legalPersonId"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="200"
        :formatter="dateFormatter"
      />
      <el-table-column
        label="详细地址"
        align="center"
        prop="detailAddress"
        width="250"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center" min-width="100" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openModel(1, scope.row.id)"
            v-hasPermi="['bid:customer:update']"
          >
            编辑
          </el-button>
          <!-- <el-button link type="primary"> 操作记录 </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>
<script setup lang="ts">
import * as approval from '@/api/approval'
import { dateFormatter } from '@/utils/formatTime'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

const dict_type = ref(DICT_TYPE)
const getIntDictOptionsFn = ref(getIntDictOptions)
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  companyName: undefined,
  contactPhone: undefined,
  state: undefined,
  biddingState: undefined,
  createTime: []
})
const queryFormRef = ref()
const emit = defineEmits(['subPage'])

const getList = async () => {
  loading.value = true
  try {
    const data = await approval.getCustomerList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const openModel = (index: number, id?: number) => {
  emit('subPage', index, id)
}

onMounted(() => {
  getList()
})
</script>
<style scoped>
.text-blue-500 {
  color: #3b82f6;
  cursor: pointer;
}
</style>
